package com.csl.jsonpreference

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.result.ActivityResultLauncher
import androidx.fragment.app.FragmentActivity
import com.csl.jsonpreference.bean.ConfigTYPE
import com.csl.jsonpreference.configdes.JsonConfigIns
import com.csl.jsonpreference.configdes.JsonObjectPreference
import com.csl.jsonpreference.tag.JsonConfig
import com.csl.jsonpreference.ui.JsonPreferenceActivity
import com.csl.jsonpreference.ui.JsonPreferenceActivity.Companion.PARAM_CLASS
import com.csl.jsonpreference.ui.JsonPreferenceDialog
import com.csl.jsonpreference.util.CLog
import org.json.JSONObject

object JsonPreferenceUtil {



//    inline fun <reified T> showConfigDialog(
//        context: FragmentActivity,
////        config: T? = null,
////        title: String? = null,
//        jsonData: String,
//        title: String = "",
//        noinline onConfigEnd: (String) -> Unit
//    ) {
//        var dialog = JsonPreferenceDialog()
//
//        config?.let {
//            dialog.configData = Config2Object.toPreference(config)
//        } ?: let {
//            dialog.configData = Config2Object.toPreference(T::class.java.newInstance() as Any)
//        }
//        title?.let {
//            dialog.configData?.discribe = it
//        }
//        dialog.resultData = T::class.java.newInstance()
//        dialog.onConfigEnd = onConfigEnd
//        dialog.show(context.supportFragmentManager, "configdialog")
//
//    }

    inline fun startConfigForDialog(
        context: FragmentActivity,
        jsonData: String,
        clazz: Class<*>? = null,
        title: String = "",
        noinline callback: ((String) -> Unit)? = null
    ) {
        var dialog = JsonPreferenceDialog()
        var objJson = jsonData
        if (objJson == null) {
            Toast.makeText(context, "配置信息错误", Toast.LENGTH_SHORT).show()
            return
        }
        CLog.d("json", "获取到的配置:$objJson, 目标类型:$clazz")
        var jsonObject = try {
            JSONObject(objJson)
        } catch (e: Exception) {
            e.printStackTrace()
            JSONObject()
        }
        var jsonConfig = JsonConfigIns(clazz?.getAnnotation(JsonConfig::class.java))
        jsonConfig.configType = ConfigTYPE.AUTO
        if(!TextUtils.isEmpty(title)) {
            jsonConfig.title = title!!
        }
        var jsonRoot = JsonObjectPreference(
            jsonObject,
            clazz,
            jsonConfig
        )
        dialog.onConfigEnd = callback
        dialog.rootFragment.configData = jsonRoot
        dialog.show(context.supportFragmentManager, "jsonPreferenceDialog")

    }

    /**
     * 启动json配置activity
     */
    inline fun startConfigForResult(
        context: Activity,
        requestCode: Int,
//        obj: Any,
        jsonData: String,
        clazz: Class<*>? = null,
        title: String = ""
    ) {
//        val clazz = T::class.java
        val intent = Intent(context, JsonPreferenceActivity::class.java)
//        var group = Config2Object.toPreference(obj)
//        var jsonStr = Gson().toJson(group)
        intent.putExtra(JsonPreferenceActivity.PARAM_JSON, jsonData)
        intent.putExtra(JsonPreferenceActivity.PARAM_TITLE, title)
        clazz?.let {
            intent.putExtra(PARAM_CLASS, clazz)
        }
        context.startActivityForResult(intent, requestCode)
    }

    /**
     * 解析json配置
     */
    inline fun <reified T> startConfigForResult(
        context: Activity,
//        obj: Any,
        jsonStr: String,
        title: String = "",
        launcher: ActivityResultLauncher<Intent>
    ) {
        val intent = Intent(context, JsonPreferenceActivity::class.java)
//        var group = Config2Object.toPreference(obj)
//        var jsonStr = Gson().toJson(group)
        val clazz = T::class.java
        intent.putExtra(JsonPreferenceActivity.PARAM_JSON, jsonStr)
        intent.putExtra(JsonPreferenceActivity.PARAM_TITLE, title)
        intent.putExtra(PARAM_CLASS, clazz)
        launcher.launch(intent)
    }

    fun getResultJson(resultCode: Int, intent: Intent?): String? {
        return if (resultCode == JsonPreferenceActivity.CODE_RESULT_SUCCESS) {
            intent?.getStringExtra(JsonPreferenceActivity.RESULT_JSON)
        } else {
            null
        }
    }


    inline fun <reified  T> createJsonFragment(jsonStr: String) {

    }
}